package com.zuxia.impDao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zuxia.iDao.RecruitrecordinfoDAOI;
import com.zuxia.po.Recruitinfo;
import com.zuxia.po.Recruitrecordinfo;
import com.zuxia.util.PageModel;

/**
 * A data access object (DAO) providing persistence and search support for
 * Recruitrecordinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.zuxia.impDao.Recruitrecordinfo
 * @author MyEclipse Persistence Tools
 */

public class RecruitrecordinfoDAO extends HibernateDaoSupport implements RecruitrecordinfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(RecruitrecordinfoDAO.class);
	// property constants
	public static final String RRINAME = "rriname";
	public static final String RRIPHONE = "rriphone";
	public static final String RRISEX = "rrisex";
	public static final String RRIID_CARD = "rriidCard";

	protected void initDao() {
		// do nothing
	}

	public void save(Recruitrecordinfo transientInstance) {
		log.debug("saving Recruitrecordinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Recruitrecordinfo persistentInstance) {
		log.debug("deleting Recruitrecordinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Recruitrecordinfo findById(java.lang.Integer id) {
		log.debug("getting Recruitrecordinfo instance with id: " + id);
		try {
			Recruitrecordinfo instance = (Recruitrecordinfo) getHibernateTemplate()
					.get("com.zuxia.impDao.Recruitrecordinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Recruitrecordinfo instance) {
		log.debug("finding Recruitrecordinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitrecordinfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Recruitrecordinfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Recruitrecordinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitrecordinfoDAOI#findByRriname(java.lang.Object)
	 */
	public List findByRriname(Object rriname) {
		return findByProperty(RRINAME, rriname);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitrecordinfoDAOI#findByRriphone(java.lang.Object)
	 */
	public List findByRriphone(Object rriphone) {
		return findByProperty(RRIPHONE, rriphone);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitrecordinfoDAOI#findByRrisex(java.lang.Object)
	 */
	public List findByRrisex(Object rrisex) {
		return findByProperty(RRISEX, rrisex);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitrecordinfoDAOI#findByRriidCard(java.lang.Object)
	 */
	public List findByRriidCard(Object rriidCard) {
		return findByProperty(RRIID_CARD, rriidCard);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitrecordinfoDAOI#findAll()
	 */
	public List findAll() {
		log.debug("finding all Recruitrecordinfo instances");
		try {
			String queryString = "from Recruitrecordinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Recruitrecordinfo merge(Recruitrecordinfo detachedInstance) {
		log.debug("merging Recruitrecordinfo instance");
		try {
			Recruitrecordinfo result = (Recruitrecordinfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Recruitrecordinfo instance) {
		log.debug("attaching dirty Recruitrecordinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Recruitrecordinfo instance) {
		log.debug("attaching clean Recruitrecordinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RecruitrecordinfoDAOI getFromApplicationContext(
			ApplicationContext ctx) {
		return (RecruitrecordinfoDAOI) ctx.getBean("RecruitrecordinfoDAO");
	}

	public Boolean addRecruitrecordinfo(Recruitrecordinfo recruitrecordinfo) {
		// TODO Auto-generated method stub
		Boolean b=true;
		log.debug("saving Recruitrecordinfo instance");
		try {
			getHibernateTemplate().save(recruitrecordinfo);
			log.debug("save successful");
		} catch (RuntimeException re) {
			b=false;
			log.error("save failed", re);
			throw re;
		}
		return b;
	}

	public Boolean addShowRecruitrecordinfo(Recruitrecordinfo recruitrecordinfo) {
		// TODO Auto-generated method stub
		Boolean b=false;
		log.debug("from Recruitrecordinfo where positioninfo.ptIid = "+recruitrecordinfo.getPositioninfo().getPtIid()+" and rriidCard= "+recruitrecordinfo.getRriidCard());
		try {
			String queryString = "from Recruitrecordinfo where positioninfo.ptIid = ? and rriidCard= ?";
			Object[] value={recruitrecordinfo.getPositioninfo().getPtIid(),recruitrecordinfo.getRriidCard()};
			if(getHibernateTemplate().find(queryString, value).size()>0)
			{
				b=true;
			}
		} catch (RuntimeException re) {
			log.error("find Recruitrecordinfo", re);
			throw re;
		}
		return b;
	}

	//分页+排序
	public List<Recruitrecordinfo> showPageModelList(int ptIid,final int pageNumber, final int limit,String order) {
		//final Object[] values
		log.debug("showPageModelList"); 
		final StringBuffer hql = new StringBuffer("from Recruitrecordinfo ");
		if(ptIid>0) {
			hql.append("where positioninfo.ptIid="+ptIid);
		}
		//排个序
		hql.append(" "+order);
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			   
			   public Object doInHibernate(Session session) throws HibernateException,
			     SQLException {
			    Query query = session.createQuery(hql.toString());
			    //这个用于扩展多条件查询用到的
			   /* for (int i = 0; i < values.length; i++) {
			     query.setParameter(i, values[i]);
			    }*/
			    int p = pageNumber;
			    int l = limit;
			    int a = (p-1)*l;
			    List result = query.setFirstResult(a)
			        .setMaxResults(l)
			        .list();
			    /*List result = query
				        .list();*/
			    System.out.println("需求dao中查询到了数据"+result);
			    return result;
			   }
			  });
		
			  return list;
	}
	
	public List showRecruitrecordinfo(int ptIid) {
		log.debug("finding all Recruitrecordinfo instances");
		try {
			final StringBuffer hql = new StringBuffer("from Recruitrecordinfo");
			if(ptIid > 0) {
				hql.append(" where positioninfo.ptIid ="+ptIid);
				System.out.println("---------"+hql.toString());
			}
			return getHibernateTemplate().find(hql.toString());
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public PageModel<Recruitrecordinfo> showRecruitrecordinfo(int ptIid,
			int pageNumber, int limit ,String order) {
		// TODO Auto-generated method stub
		List<Recruitrecordinfo> list=this.showPageModelList(ptIid, pageNumber, limit,order);
		PageModel<Recruitrecordinfo> p=new PageModel<Recruitrecordinfo>(this.showRecruitrecordinfo(ptIid).size(), pageNumber, limit);
		p.setList(list);
		return p;
	}

	public Boolean deleteRecruitrecordinfo(int rrid) {
		// TODO Auto-generated method stub
		Boolean b=true;
		log.debug("deleting Recruitrecordinfo instance");
		try {
			Recruitrecordinfo r= getHibernateTemplate().get(Recruitrecordinfo.class,rrid);
			getHibernateTemplate().delete(r);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			b=false;
			log.error("delete failed", re);
			throw re;
		}
		return b;
	}
}